//树的高度
$(".vany_authority .ztree").css({
	"max-height": $(window).height() - 310
})
//开启节点
function EnableUse(obj) {
	obj.$confirm('确定开启该角色功能?', '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning'
	}).then(function() {
		zTree.getSelectedNodes()[0].state = 1;
		$("#" + zTree.getSelectedNodes()[0].tId).find("a").css({
			"text-decoration": "none",
			"cursor": "pointer",
			"color": "#333"
		});
	}).catch(function() {
		obj.$message({
			type: 'info',
			message: '已取消开启功能'
		});
	});
}
//禁用节点
function DisableUse(obj) {
	obj.$confirm('确定禁用该角色功能?', '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning'
	}).then(function() {
		zTree.getSelectedNodes()[0].state = 0;
		$("#" + zTree.getSelectedNodes()[0].tId).find("a").css({
			"text-decoration": "line-through",
			"cursor": "no-drop",
			"color": "#999"
		});
	}).catch(function() {
		obj.$message({
			type: 'info',
			message: '已取消禁用功能'
		});
	});
}

//上移节点
var curSrcNode;

function UpTreeNode() {
	VanyAjax("/base/org/updateOrderNum", {
		orgCode: zTree.getSelectedNodes()[0].id,
		OrgOrderNumber: zTree.getSelectedNodes()[0].tId,
		action: "UP"
	}).done(function(data) {
		hideRMenu();
		var nodes = zTree.getSelectedNodes();
		if(curSrcNode) {
			delete curSrcNode.isCur;
			var tmpNode = curSrcNode;
			curSrcNode = null;
		}
		curSrcNode = nodes[0];
		if(!nodes[0]) return;

		curSrcNode.isCur = true;
		zTree.cancelSelectedNode();

		var prevnode, targetNode;
		if(nodes.length > 0) {
			prevnode = nodes[0].getPreNode();
		}
		if(!prevnode == '') {
			targetNode = prevnode;
		} else {
			alert('无法再向上移动了哦');
			return false;
		}
		targetNode = zTree.moveNode(targetNode, curSrcNode, "prev");
		targetNode = curSrcNode;
		setCurSrcNode();
		delete targetNode.isCur;
	}, false, this);
}

function setCurSrcNode(treeNode) {
	if(curSrcNode) {
		delete curSrcNode.isCur;
		var tmpNode = curSrcNode;
		curSrcNode = null;
	}
	curSrcNode = treeNode;
	if(!treeNode) return;

	curSrcNode.isCur = true;
	zTree.cancelSelectedNode();
}

//下移节点
function DownTreeNode() {
	VanyAjax("/base/org/updateOrderNum", {
		orgCode: zTree.getSelectedNodes()[0].id,
		OrgOrderNumber: zTree.getSelectedNodes()[0].tId,
		action: "DOWN"
	}).done(function(data) {
		hideRMenu();
		var nodes = zTree.getSelectedNodes();
		if(curSrcNode) {
			delete curSrcNode.isCur;
			var tmpNode = curSrcNode;
			curSrcNode = null;
		}
		curSrcNode = nodes[0];
		if(!nodes[0]) return;

		curSrcNode.isCur = true;
		zTree.cancelSelectedNode();
		var Nextnode, targetNode;
		if(nodes.length > 0) {
			Nextnode = nodes[0].getNextNode();
		}
		if(!Nextnode == '') {
			targetNode = Nextnode;
		} else {
			alert('无法再向下移动了哦');
			return false;
		}
		targetNode = zTree.moveNode(targetNode, curSrcNode, "next");
		targetNode = curSrcNode;
		setCurSrcNode();
		delete targetNode.isCur;
	}, false, this);
}
//人员排序
/*本部门排序*/
function sortOrg(elId, url) {
	Vue.component(elId, {
		functional: true,
		render: function(h, ctx) {
			var item = ctx.props.item;
			return h('li', ctx.data, [
				h('div', {
					attrs: {
						class: 'name'
					}
				}, [item.userName]),
				h('span', {
					attrs: {
						class: 'job'
					}
				}, [item.jobPosition])
			]);
		},
		props: {
			item: {
				type: Object,
				required: true
			}
		}
	});
	var sortOrgPersons = new Vue({
		el: '#' + elId,
		data: function() {
			return {
				dialogTableVisible: false,
				orgPersons: [],
				curSeletedPersonIndex: 0,
				params: {
					orgCode: "",
					pageIndex: 1,
					searchValue: '',
					status: '',
					pageSize: 100
				},
				matches: ""
			}
		},
		methods: {
			saveSorted: function() {
				console.log("保存");
			},
			/**
			 * 获取排序人员数据
			 */
			getOrgPersons: function() {
				VanyAjax(url, this.params, "GET").done(function(data) {
					this.orgPersons = data;
					this.dialogTableVisible = true;
				}, false, this);
			},
			/**
			 * 交换数组相邻元素
			 * Number src: 源
			 * Number to：到
			 * 对于to→src的交换请转换为src→to入参
			 * 有副作用：该方法会修改传入的数组
			 */
			swapArrayItems: function(array, src, to) {
				array.splice(to, 2, array[src], array[to]);
			},
			/**
			 * 高亮表格某行
			 * Number oldindex: 原来高亮位置
			 * Number newindex: 现在高亮位置
			 */
			updateCurSeletedByHand: function(oldindex, newindex) {
				var trs = this.$el.getElementsByTagName("tr");
				if(oldindex !== null || oldindex !== undefined) {
					trs[oldindex + 1].className = "el-table__row";
				}
				if(oldindex !== null || oldindex !== undefined) {
					trs[newindex + 1].className = "el-table__row current-row";
				}
			},
			updateOrderNum: function(action, fn) {
				VanyAjax("/base/user/updateOrderNum", {
					orgCode: this.params.orgCode,
					action: action,
					userId: this.orgPersons[this.curSeletedPersonIndex].userId
				}).done(function(data) {
					fn.call(this);
				}, true, this);
			},
			/**
			 * 移动人员
			 * String postion: top up down bottom
			 */
			movePersonTo: function(postion) {
				var index = this.curSeletedPersonIndex;
				switch(postion) {
					case "top":
						if(index === 0) {
							return;
						}
						this.updateOrderNum(postion, function() {
							this.orgPersons.unshift(this.orgPersons.splice(index, 1)[0]);
							this.updateCurSeletedByHand(index, 0);
							this.curSeletedPersonIndex = 0;
						});
						break;
					case "up":
						if(index === 0) {
							return;
						}
						this.updateOrderNum(postion, function() {
							this.swapArrayItems(this.orgPersons, index, index - 1);
							this.updateCurSeletedByHand(index, index - 1);
							this.curSeletedPersonIndex = index - 1;
						});
						break;
					case "down":
						if(index === this.orgPersons.length - 1) {
							return;
						}
						this.updateOrderNum(postion, function() {
							this.swapArrayItems(this.orgPersons, index + 1, index);
							this.updateCurSeletedByHand(index, index + 1);
							this.curSeletedPersonIndex = index + 1;
						});
						break;
					case "bottom":
						if(index === this.orgPersons.length - 1) {
							return;
						}
						this.updateOrderNum(postion, function() {
							this.orgPersons.push(this.orgPersons.splice(index, 1)[0]);
							this.updateCurSeletedByHand(index, this.orgPersons.length - 1);
							this.curSeletedPersonIndex = this.orgPersons.length - 1;
						});
						break
				}
			},
			/**
			 * 记录当前选中人员所在的索引
			 */
			recordSeletedPerson: function(row, event, column) {
				for(var i = 0; i < this.orgPersons.length; i++) {
					if(this.orgPersons[i].userId === row.userId) {
						this.curSeletedPersonIndex = i;
						return this.curSeletedPersonIndex;
					}
				}
			},
			/**
			 * 按关键字搜素
			 */
			querySearch: function(queryString, cb) {
				var orgPersons = this.orgPersons;
				var results = queryString ? orgPersons.filter(this.createFilter(queryString)) : orgPersons;
				// 调用 callback 返回建议列表的数据
				cb(results);
			},
			/**
			 * 搜索过滤器
			 */
			createFilter: function(queryString) {
				console.log(queryString)
				return function(personInfo) {
					if(!personInfo.jobPosition) {
						personInfo.jobPosition = "";
					}
					return personInfo.userName.indexOf(queryString) > -1 || personInfo.jobPosition.indexOf(queryString) > -1 || personInfo.userId.indexOf(queryString) > -1;
				};
			},
			/**
			 * 搜素结果选择
			 */
			handleSelect: function(item) {
				this.$refs["orgPersonsTable"].setCurrentRow(item);
				this.curSeletedPersonIndex = this.recordSeletedPerson(item);
			},
			handleClose: function(done) {
				this.$confirm('确认关闭？', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(function() {
					done();
				}).catch(function() {})
			}
		}
	});
	return sortOrgPersons;
}

//人员展示
function RoleOrg(elId, url) {
	// 角色人员对话框
	var rolePerson = new Vue({
		el: '#' + elId,
		data: function() {
			return {
				rolePersonVisible: false,
				tableData: [],
				_tableData: [],
				keyWord: "",
				sels: []
			}
		},
		methods: {
			getRolePerson: function() {
				this.$http({
					method: 'GET',
					url: url
				}).then(function(response) {
					this.tableData = response.data;
					this._tableData = [].concat(this.tableData);
					this.rolePersonVisible = true;
				}, function(error) {
					console.log(error)
				})
			},
			remove: function() {
				for(var i = 0; i < this.sels.length; i++) {
					for(j = 0; j < this._tableData.length; j++) {
						if(this.sels[i].account_number === this._tableData[j].account_number) {
							this._tableData.splice(j, 1);
							break;
						}
					}
				}
				for(var i = 0; i < this.sels.length; i++) {
					for(j = 0; j < this.tableData.length; j++) {
						if(this.sels[i].account_number === this.tableData[j].account_number) {
							this.tableData.splice(j, 1);
							break;
						}
					}
				}
			},
			selsChange: function(sels) {
				this.sels = sels;
			},
			genderFormatter: function(row, column) {
				return row.Gender ? "男" : "女"
			},
			/**
			 * 按搜素关键字过滤表格数据
			 */
			filterTable: function() {
				if(this._tableData.length === 0) {
					return;
				}
				var self = this,
					results = this.keyWord ? this._tableData.filter(function(item) {
						return self.createFilter(item, this.keyWord);
					}) : this._tableData;
				this.tableData = results;
			},
			/**
			 * 表格数据过滤器
			 * 过滤对象: Obj item
			 * return Bool
			 */
			createFilter: function(item, state) {
				for(var key in item) {
					if(item[key].toString().indexOf(this.keyWord) > -1) {
						return true
					}
				}
				return false;
			},
			handleClose: function(done) {
				this.$confirm('确认关闭？', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(function() {
					done();
				}).catch(function() {})
			}
		}
	})
	return rolePerson;
}